package models

import (
	"labix.org/v2/mgo/bson"
	"time"
)

type UserInfo struct {
	Id       string `bson:"_id,omitempty"`
	Name     string
	Password string
	Email    string
	IniTime  time.Time
}

//新增一用户
func (this *UserInfo) Add() error {
	defer close()
	defer exption()
	c := open("UserInfo")
	this.IniTime = time.Now()
	err := c.Insert(this)
	return err
}

//通过用户名密码找用户
func (this *UserInfo) FindUser() error {
	defer close()
	defer exption()
	c := open("UserInfo")
	err := c.Find(bson.M{"name": this.Name, "password": this.Password}).One(this)
	return err
}

//判断用户名是否存在
func (this *UserInfo) IsExists() bool {
	defer close()
	defer exption()
	c := open("UserInfo")
	err := c.Find(bson.M{"name": this.Name}).One(this)
	if err != nil || this.Id == "" {
		return false
	}
	return true
}

//通过id获取用户信息
func (this *UserInfo) Get() error {
	defer close()
	defer exption()
	c := open("UserInfo")
	err := c.Find(bson.M{"_id": this.Id}).One(this)
	return err
}
